package com.service.impl;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.ibatis.annotations.ResultMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.dao.ItemDAO;
import com.pojo.Item;
import com.pojo.Page;
import com.service.ItemService;

@Service
public class ItemServiceImpl implements ItemService {
	@Autowired
	ItemDAO itemDao;

	public Map<String, Object> getItemList(Page page) throws Exception {
		List<Item> itemList = itemDao.findItemList(page);
		Integer totalItems = itemDao.findItemCount();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("itemList", itemList);
		resultMap.put("totalItems", totalItems);
		return resultMap;
	}

	public Item getItem(int id) throws Exception {
		return itemDao.findItemById(id);
	}

	public Item getItem(Item item) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	public void saveItemPicture(Item item, MultipartFile itemPic,String rootPath) throws Exception {
		String originalFileName = itemPic.getOriginalFilename();
		if (itemPic != null && originalFileName != null && originalFileName.length() > 0) {
			String picPhysicalPathOnServer = rootPath+"jsp\\image\\";
			String picFileOnServerName = UUID.randomUUID()
					+ originalFileName.substring(originalFileName.indexOf("."));
			File picFileOnServer = new File(picPhysicalPathOnServer + picFileOnServerName);
			itemPic.transferTo(picFileOnServer);
			item.setPic(picFileOnServerName);
			System.out.println(item);
		}
	}

}
